package com.wangshuo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangshuo.common.JwtHelper;
import com.wangshuo.config.Result;
import com.wangshuo.entity.Goods;
import com.wangshuo.entity.GoodsItem;
import com.wangshuo.service.GoodsService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodController {
    @Resource
    private GoodsService goodsService;
    @Resource
    private JwtHelper jwtHelper;

    @PostMapping("/add")
    public Result add(@RequestBody Goods goods, HttpServletRequest httpServletRequest) {
        Long uid = jwtHelper.getUserId(httpServletRequest.getHeader("token"));
        goods.setUid(Math.toIntExact(uid));
        goodsService.saveOrUpdate(goods);
        return Result.ok("成功");
    }
    @GetMapping("/list")
    public Result list(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String keyword,HttpServletRequest httpServletRequest){
        IPage<GoodsItem> page=new Page<>(pageNum,pageSize);
        Long uid = jwtHelper.getUserId(httpServletRequest.getHeader("token"));
        IPage<GoodsItem> list=goodsService.getpage(page,keyword,uid);
        return Result.ok(list);
    }
    @GetMapping("/alllist")
    public Result list(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String keyword){
        IPage<GoodsItem> page=new Page<>(pageNum,pageSize);

        IPage<GoodsItem> list=goodsService.getpage1(page,keyword);
        return Result.ok(list);
    }

    @GetMapping("/delete")
    public Result delete(@RequestParam Long id){
        goodsService.removeById(id);
        return Result.ok("成功");
    }
    @GetMapping("/detail")
    public Result detail(@RequestParam Long id){
        GoodsItem goods=goodsService.getbyid(id);
        return Result.ok(goods);
    }
}
